<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 用户管理
 */
class TicketController extends BaseController
{
    /**
     * 用户列表
     * @return [type] [description]
     */
    public function index($key="")
    {
        $where = array();
        if($key == ""){
            $model = M('ticket');
        }else{
            //$where['username'] = trim($key);
            //$model = M('jubao')->where($where); 
        }


        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('member', $member);
        $this->assign('page',$show);
        $this->display();
    }

    /**
     * 更新管理员信息
     * @param  [type] $id [管理员ID]
     * @return [type]     [description]
     */
    public function update()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $id = I('id');
            $info = M('Ticket')->find($id);
            $this->assign('info',$info);
            $list = M('Ticketreply')->where(array('tid'=>$id))->order('id asc')->select();
            $this->assign('list',$list);
            $this->fid = session('adminId');
            $this->display();
        }
        if (IS_POST) {
            $id = I('post.id');
            $tid = $id;
            if(!trim(I('post.desc'))){
                $this->error('回复不能为空');exit;
            }
            $nowtime = time();
            $uid = session('adminId');
            $data = array();
            $data['status'] = "1";
            $tid = M("Ticket")->where(array("id"=>$id))->save($data);
            if($tid != false){
                $data = array();
                $data['desc'] = trim(I('post.desc'));
                $data['addtime'] = date('Y-m-d H:i:s',$nowtime);
                $data['userid'] = $uid;
                $data['tid'] = $id;
                $trid = M("Ticketreply")->add($data);
                if($trid !=flase){
                    $this->success("回复成功", U('ticket/index'));
                }else{
                    $this->error("回复失败", U('ticket/index'));
                }
            }else{
                $this->success("该信息已经回复过", U('ticket/index'));
            }
        }
    }

    /**
     * 删除管理员
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        if(C('SUPER_ADMIN_ID') == $id) $this->error("超级管理员不可禁用!");
        $model = M('admin');
        //查询status字段值
        $result = $model->find($id);
        //更新字段
        $data['id']=$id;
        if($result['status'] == 1){
            $data['status']=0;
        }
        if($result['status'] == 0){
            $data['status']=1;
        }
        if($model->save($data)){
            $this->success("状态更新成功", U('admin/index'));
        }else{
            $this->error("状态更新失败");
        }

    }

    /**
     * 批量删除
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function alldel($id) {

        if (empty($id)) {
            $this->error("不能提交空值!");
        }
        $id=$this->jiexi($id);

        $member = M("ticket");
        $result = $member->where("id in($id)")->field()->select();
        $post = M('ticketreply');

        $hobby = $post->where("tid in($id)")->field('id')->select();

        foreach ($hobby as $k => $val) {

            $map['id'] = $val['id'];

            $res = $post->where($map)->delete();

        }

        $memenum = $member->where("id in($id)")->delete($id);
        if($memenum){

            //unlink('./Public/uploads/'.$info['uptxt']);
            $this->success("删除成功", U('ticket/index'));
        }else{
            $this->error("删除失败", U('ticket/index'));
        }
    }
    public function jiexi($id)
    {
        //解析参数
        $arr = explode(",", $id);
        //如果有多余字符去掉
        $arr = array_merge(array_diff($arr, array("undefined")));
        $arr = array_merge(array_diff($arr, array("on")));

        if (is_array($arr)) {
            return $id = implode(',', $arr);
        } else {
            return $id = 'id=' . $arr;
        }
    }


}
